class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/sqlc-dev/sqlc/archive/v1.20.0.tar.gz"
  sha256 "65d1897709da9691ffad49211b2fa29ee86d10e0ab215f54fc80c5c3080e439c"
  license "MIT"
  head "https://github.com/sqlc-dev/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_ventura:  "3617aa6dc2071f503536cb835bc06de57cec44436f87e2f54b7cc95d92312e01"
    sha256 cellar: :any_skip_relocation, arm64_monterey: "b121519a8fed3d2e3c40ead08e2d7b347c98427421e7f0f899e20523d6c50535"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "4e6e063686efa0e32c899677f1052a9c921c1a816cd06ba05966a185e3ded3a2"
    sha256 cellar: :any_skip_relocation, ventura:        "d4c9570d6abf1d86e0ec90150d538182ee216d2b224503fe3214016b21f0f59d"
    sha256 cellar: :any_skip_relocation, monterey:       "f2a9b3db40397479efe0c3204043ef9398d014d7c2eb72687dc5aaa09c10badb"
    sha256 cellar: :any_skip_relocation, big_sur:        "bbf4786862a1421eda4780939e3e2311c3481e79c67a378234833b69e1a6b0a3"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "6344ee2e92971007dfb4365cf44ceef3c6db5f1bb4aa9219c3db319e0556a3ee"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
